02. Working with documents
ND#305 C03 L04 A03 Create Documents
Exercise
Task Description:
Environment Setup
MongoDB server is started.
Java 1.8 is installed.
Checkout the starter code from <
Import the starter code into the IDE like IntelliJ IDEA CE (Optional).
Modify the Application.java
class to complete the exercise.
Document Structure
The member document represents the members of a health club.
{
"_id": “cjenkins”,
"first_name": "Carl",
"last_name": "Jenkins",
"gender": "male",
"age": 23,
"address": {
"street": "123 Main Street",
"city": "Oakland",
"state": "CA"
},
"interests": ["pilates","swim","crossfit"],
"balance": 125.20
}
How to run the code?
In the IDE, Run the Exercise4Application
class.
Using Maven
MacOS - ./mvnw exec:java
Windows - mvnw exec:java
Task Feedback:
Well done! You now know how to create,update and delete documents in a collection using the mongo driver.
ND#305 C03 L04 A04 Querying Documents
Exercise
Task Description:
Environment Setup
MongoDB server is started.
Use mongo shell to connect to your local MongoDB server.
Select the database jdnd-c3
.
Run db.members.remove({})
to remove all documents in the collection.
Insert seed data given below.
Checkout the starter code from <
Import the starter code into the IDE like IntelliJ IDEA CE (Optional).
Modify the Application.java
class to complete the exercise.
Seed Data
db.members.insert([{
first_name: "Jane",
last_name: "Doe",
age: 45,
gender: "female",
interests: ["pilates","swim","crossfit"],
balance: 125.20,
address: {
street: "123 Main St",
city: "Birmingham",
state: "AL"
}
},
{
first_name: "John",
last_name: "Doe",
age: 47,
gender: "male",
interests: ["pilates","swim"],
balance: 56.25,
address: {
street: "123 First St",
city: "Minneapolis",
state: "MN"
}
},
{
first_name: "Lakshmi",
last_name: "Natarajan",
age: 29,
gender: "female",
interests: ["swim", "tennis"],
balance: 556.39,
address: {
street: "149 Main St",
city: "Birmingham",
state: "AL"
}
},
{
first_name: "Eduardo",
last_name: "Lopez",
age: 32,
gender: "male",
interests: ["swim", "tennis", "golf"],
balance: 1034.23,
address: {
street: "298 Second St",
city: "Birmingham",
state: "AL"
}
},
{
first_name: "Sana",
last_name: "Khan",
age: 53,
gender: "female",
interests: ["golf"],
balance: 500,
address: {
street: "649 First St",
city: "Minneapolis",
state: "MN"
}
},
{
first_name: "Rahul",
last_name: "Mani",
age: 18,
gender: "male",
interests: ["swim"],
balance: 250,
address: {
street: "41 Second Ave",
city: "Indianapolis",
state: "IN"
}
}
]);
How to run the code?
In the IDE, Run the Application
class.
Using Maven
MacOS - ./mvnw exec:java
Windows - mvnw exec:java
Task Feedback:
Well done! You used the various query methods in MongoDB java driver.